﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.Orders;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.Order;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.Order
{
    public class PrepayOrderMainRepository : SqlServerRespository<PrePayOrderMainInfoEntity>, IPrePayOrderMainRepository
    {
        public PrepayOrderMainRepository()
        {
            Context = new WechatMallContext();
        }

        public void DeleteOrder(string sno, UnitOfWork unit = null)
        {
            DapperHelper.Execute("delete from weiscdzfddt WHERE ddlsh=@sno", Context, unit, new {sno});
        }

        public void AddMainInfo(PrePayOrderMainInfoEntity entity, UnitOfWork unit = null)
        {
            Add(entity, unit);
        }


        public PrePayOrderMainInfoEntity GetOrderMainBySno(string sno)
        {
          
            var sql = @"SELECT  ddlsh AS Sno ,
                        dddjh AS Bno ,
                        dddjlx AS SellType ,
                        ddjffs AS DeliveryType ,
                        ddhsr AS Billdate ,
                        ddwsckhid AS Custid ,
                        ddkhzklbid AS MemberLevel ,
                        ddhj AS TotalMy ,
                        ddysjehj AS DealMy ,
                        ddyspsfy AS FeeMy ,
                        dddjbcsj AS UpdatedTime ,
                        ddshopid AS Shopid,
                        ddshopname as Shopname
                        FROM    weiscdzfddt WHERE ddlsh=@sno";

            var first = GetList(sql, new {sno}).FirstOrDefault();
            return first;
        }

        public IList<PrePayOrderMainInfoEntity> GetOrders(long custid)
        {
            var sql = @"SELECT  ddlsh AS Sno ,
                        dddjh AS Bno ,
                        dddjlx AS SellType ,
                        ddjffs AS DeliveryType ,
                        ddhsr AS Billdate ,
                        ddwsckhid AS Custid ,
                        ddkhzklbid AS MemberLevel ,
                        ddhj AS TotalMy ,
                        ddysjehj AS DealMy ,
                        ddyspsfy AS FeeMy ,
                        dddjbcsj AS UpdatedTime ,
                        ddshopid AS Shopid,
                        ddshopname as Shopname
                        FROM    weiscdzfddt WHERE ddwsckhid=@custid order by ddlsh desc";
         
            return GetList(sql, new {custid}).ToList();
        }
    }
}